package wyh.service;

import org.apache.ibatis.session.SqlSession;
import wyh.mapper.UserMapper;
import wyh.pojo.User;
import wyh.util.MyBatisUtils;

import java.util.List;

public class UserService {
    private static SqlSession sqlSession = null;
    private static UserMapper userMapper = null;

    public void beforeEach() {
        sqlSession = MyBatisUtils.getSession(true);
        userMapper = sqlSession.getMapper(UserMapper.class);
    }

    //        验证用户账号
    public User ServicevalidateUser(String username, String password) {
        User user = null;
        beforeEach();
        user = userMapper.validateUser(username, password);
        sqlSession.close();
        return user;
    }


    /**
     * @param username 模糊查询的用户名
     * @return 返回一个用户对象列表
     */
    public List<User> ServicegetByNameLike(String username) {
        beforeEach();
        List<User> userList = userMapper.getByNameLike(username);
        sqlSession.close();
        return userList;
    }

    public List<User> ServicegetAll() {
        beforeEach();
        List<User> userList = userMapper.getAll();
        sqlSession.close();
        return userList;
    }

    public Boolean ServicedeleteById(Integer user_id) {
        beforeEach();
        int result = userMapper.deleteById(user_id);
        sqlSession.close();
        return result > 0;
    }

    public Boolean Serviceupdate(User user) {
        beforeEach();
        int result = userMapper.updateById(user);
        sqlSession.close();
        return result > 0;
    }

    public Boolean Serviceadd(User user) {
        beforeEach();
        int result = userMapper.addUser(user);
        sqlSession.close();
        return result > 0;
    }

    public User ServicegetById(Integer user_id) {
        beforeEach();
        User user = userMapper.getById(user_id);
        sqlSession.close();
        return user;
    }
}